<?php

namespace app\controllers;

use Yii;
use app\models\Comments;
use app\models\Qa;
use app\models\Article;
use app\models\Users;
use app\controllers\BaseController;

class CommentsController extends BaseController {

    private $type = array('qa', 'article');

    public function actionIndex() {
        return $this->render('index');
    }

    /**
     * 添加评论
     * @return type
     */
    public function actionAdd() {
        $session = Yii::$app->session;
        if (Yii::$app->user->isGuest) {
            return $this->redirect('/user/login');
        }

        $get = Yii::$app->request->get();
        $post = Yii::$app->request->post();

        //1、判断问答是否为自己添加
        switch ($get['type']) {
            case 'qa':
                $result = Qa::findOne(['id' => $get['id'], 'uid' => Yii::$app->user->identity->id]);
                $message = '不能回答自己提的问题哦！';
                break;
            case 'article':
                $result = Article::findOne(['id' => $get['id'], 'uid' => Yii::$app->user->identity->id]);
                $message = '不能评论自己发的文章哦';
                break;
            default:
                $result = FALSE;
                break;
        }
        if ($result) {
            $session->setFlash('message', $message);
            return $this->redirect('/site/message');
        }
        //2、判断是否已经评论过了
        $result = Comments::findOne(['type' => $get['type'], 'rid' => $get['id'], 'uid' => Yii::$app->user->identity->id]);
        if ($result) {
            $session->setFlash('message', '你已经回答过了，不能重复回答！');
            return $this->redirect('/site/message');
        }

        //3、保存评论
        if (in_array($get['type'], $this->type) && intval($get['id'])) {
            $model = new Comments();
            $model->type = $get['type'];
            $model->rid = $get['id'];
            $model->body = $post['Comments']['body'];
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
                if ($get['type'] == 'qa') {
                    return $this->redirect(['/qa/view', 'id' => $get['id']]);
                } elseif ($get['type'] == 'article') {
                    return $this->redirect(['/article/view', 'id' => $get['id']]);
                }
            } else {
                $session->setFlash('message', '评论失败，请联系管理员！');
                return $this->redirect('/site/message');
            }
        } else {
            return $this->redirect(['/qa/view', 'id' => $get['id']]);
        }
    }

    /**
     * 评论展示
     * @param int $id
     * @return type
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionView($id) {
        //1、获取评论内容
        $comment = Comments::findOne($id);
        if (!$comment) {
            throw new \yii\web\NotFoundHttpException('不存在的评论');
        }
        //2、获取与评论相关的内容
        $content = Comments::content($comment['type'], $comment['rid']);
        //3、获取与评论相关的内容作者
        $author = Users::findOne($content['uid']);

        return $this->render('view', ['comment' => $comment, 'content' => $content, 'author' => $author]);
    }

}
